
%% Run this script to initialize PMTK3

% This file is from pmtk3.googlecode.com
 
set(0,'DefaultAxesFontSize',15,'DefaultTextFontSize',15);

format compact
more   off; % especially important in Octave - init may fail without it
verbose = true;
if verbose
    disp('initializing pmtk3'); 
end

isMatlab = ~isempty(ver('matlab')); 
if ~isMatlab
    error('run initPmtk3Octave instead')
end


%% change to the directory storing this function, (should be PMTK3 root).
w = which(mfilename()); 
thisDir = fileparts(w);
cd(thisDir);
addpath(thisDir);

%% include directories
%include  = @(d, varargin)addpath(genpathPMTK(d, isMatlab), varargin{:}); 
include  = @(d)addpath(genpathPMTK(d)); 
include(fullfile(thisDir, 'projects')); 
include(fullfile(thisDir, 'pmtkTools')); 
include(fullfile(thisDir, 'matlabTools')); 
include(fullfile(thisDir, 'toolbox')); 
include(fullfile(thisDir, 'demos')); 
include(fullfile(thisDir, 'data'));
include(fullfile(thisDir, 'bigData'));
%include(fullfile(thisDir, 'pmtkdataSmall')); 
%include(fullfile(thisDir, 'pmtkdataCopy'));             % may be initially empty
include(fullfile(thisDir, 'pmtksupportCopy')); % may be initially empty
if exist(fullfile(thisDir, 'docs', 'tutorial'), 'dir')
    include(fullfile(thisDir, 'docs', 'tutorial')); 
end


%% include PMTK support
source = getConfigValue('PMTKlocalSupportPath');
if exist(source, 'dir')
    include(source); 
    fprintf('adding %s to path\n', source);
end
if ~(exist('pmtkSupportRoot', 'file') == 2)
    downloadAllSupport(fullfile(pmtk3Root(), 'pmtksupportCopy'), false);
end

%% include graphViz 
if  ~verLessThan('matlab', '7.6.0')
    gvizDir = getConfigValue('PMTKgvizPath'); 
    if exist(gvizDir, 'dir')
       addtosystempath(gvizDir);  
       fprintf('adding %s to path\n', gvizDir);
    end
end

%% add various binary executables to the system path.
if exist('pmtkSupportRoot', 'file')
    folder = pmtkSupportRoot(); 
    dirs   = {'svmLightWindows'        , ...
             'liblinear-1.51\windows' , ...
             'libsvm-mat-2.9.1', ...
             'mplp-1.0'};
    for i=1:length(dirs)
        addtosystempath(fullfile(folder, dirs{i}))
    end
end

%% add local data directory to path if it exists
source = getConfigValue('PMTKlocalDataPath');
if exist(source, 'dir')
    include(source); 
    fprintf('adding %s to path\n', source);
end

%% Generate some function files concerning user's setup  
pmtkInfoDir = fullfile(thisDir, 'pmtkTools', 'systemInfo');

% write isOctave.m
text = { 'function answer = isOctave()'
         '%autogenerated by initPmtk3()'
              sprintf('answer = %d;', ~isMatlab);
         'end'
        };
writeText(text, fullfile(pmtkInfoDir, 'isOctave.m'));

% write libdaiInstalled.m
installed = (exist('dai', 'file') == 3);
text = { 'function answer = libdaiInstalled()'
         '%autogenerated by initPmtk3()'
              sprintf('answer = %d;', installed);
         'end'
        };
writeText(text, fullfile(pmtkInfoDir, 'libdaiInstalled.m'));

% write UGMInstalled.m
installed = (exist('UGM_makeNodePotentialsC', 'file') == 3);
text = { 'function answer = UGMInstalled()'
         '%autogenerated by initPmtk3()'
              sprintf('answer = %d;', installed);
         'end'
        };
writeText(text, fullfile(pmtkInfoDir, 'UGMInstalled.m'));


% write glmnetInstalled.m
% Even if the binary file exists, it may fail to run
% e.g. on MAC OS X, the .mexmaci64 file exists by the fortran library 
% /usr/local/lib/libgfortran.2.dylib is missing
installed = true;
try
    x=randn(100,20); y=randn(100,1); fit=glmnet(x,y);
catch
    installed = false;
end
text = { 'function answer = glmnetInstalled()'
         '%autogenerated by initPmtk3()'
              sprintf('answer = %d;', installed);
         'end'
        };
writeText(text, fullfile(pmtkInfoDir, 'glmnetInstalled.m'));


% write svmInstalled.m
% The binary file might not exist for some OS eg., libsvm has no mexmaci64
% svmFit could call somethign else instead
installed = true;
try
    x=randn(10,20); y=randn(10,1);
    model = svmFit(x, y, 'C', 1,'kernelParam', 1, 'kernel', 'rbf'); 
catch
    installed = false;
end
text = { 'function answer = svmInstalled()'
         '%autogenerated by initPmtk3()'
              sprintf('answer = %d;', installed);
         'end'
        };
writeText(text, fullfile(pmtkInfoDir, 'svmInstalled.m'));



% write is*ToolboxInstalled.m functions
toolbox = {'stats', 'bioinfo', 'optim', 'signal', 'images', 'symbolic',...
    'nnet', 'splines'};
for t=1:numel(toolbox)
    if isMatlab
        installed = ...
            onMatlabPath(fullfile(matlabroot, 'toolbox', toolbox{t}), true);
    else
        installed = false;
    end
    fname =   sprintf('%sToolboxInstalled', toolbox{t});
    text  = { sprintf('function answer = %s()', fname)
        '% autogenerated by initPmtk3()'
        sprintf('answer = %d;', installed)
        'end'
        };
    writeText(text, fullfile(pmtkInfoDir, [fname, '.m']));
end


%% compile mex
if false % automated compilation does not work on all systems since the
         % Matlab versions for these OSs do not come with a built in
         % compiler, e.g. 64bit windows.
    
    % We use the existence of the 'mexDone.txt' as a marker so that we only
    % check for uncompiled mex files the first time this is run.
    if isMatlab
        alreadyIncluded = {'mexw32', 'mexglx'}; % compiled files are already included for these types
        checkFile =  fullfile(pmtk3Root(), 'localUtil', 'mexDone.txt');
        if ~ismember(mexext(), alreadyIncluded) && ~exist(checkFile, 'file')
            installLightspeedPMTK();
            compileC(pmtk3Root()); % does not compile external packages, most of which require special linkage
            writeText({'Autogenerated by initPmtk3'}, checkFile);
        end
    end
end

%%
if verbose, 
    disp('welcome to pmtk3'); 
end
